package cc.vv.btpartyjob.jnimodule.activity

import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.http.BaseHttpRequest
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.R
import cc.vv.btpartyjob.jnimodule.adapter.SimpleTreeAdapter
import cc.vv.btpartyjob.jnimodule.api.JniModuleAPI
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.delegate.OrganizationActivityDelegate
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.moudel.bean.org.*
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.Node
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.TreeListViewAdapter
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.ZtreeNodesBean
import cc.vv.btpartyjob.jnimodule.moudel.req.OrgRequestObj
import cc.vv.btpartyjob.jnimodule.moudel.resp.OrgBtQuResponseObj
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 组织查询
 * Created by Chen.qi on 2018/10/30.
 */
class OrganizationActivity : BaseActivityMVP<OrganizationActivityDelegate>() {
    private var mAdapter: SimpleTreeAdapter? = null
    private val list = ArrayList<Node>()

    /**
     * 组织架构展示列表类型
     * 0:全部
     * 1：街道
     * 2：社区
     * 3.网格
     * 4.单位
     * 5.社会组织
     * 6：楼栋党小组
     * 7:党员
     * 8：单元中心户
     */
    private var orgType = 0

    /**
     * 组织架构选择类型
     * 0:全部
     * 1：街道
     * 2：社区
     * 3.网格
     * 4.单位
     * 5.社会组织
     * 6：楼栋党小组
     * 7:党员
     * 8：单元中心户
     */
    private var chooseType = 0

    /**
     * 是否传回所有的路径信息和街道-社区-网格-----等等
     */
    private var isAllPath = false

    private var defaultLevel: Int = 2


    override fun initData() {
        viewDelegate?.setTopBarTitle("组织架构")
        try {
            orgType = intent.getIntExtra(IntentJniKey.INTENT_KEY_ORG_TYPE, 0)
            chooseType = intent.getIntExtra(IntentJniKey.INTENT_KEY_ORG_CHOOSE, 0)
            isAllPath = intent.getBooleanExtra(IntentJniKey.INTENT_KEY_ORG_ALL_PATH, false)
//            defaultLevel = intent.getIntExtra(IntentJniKey.INTENT_KEY_ORG_LEVEL, 2)
        } catch (e: Exception) {
        }
        mAdapter = SimpleTreeAdapter(viewDelegate?.getListView(), this, list, defaultLevel, chooseType)
        viewDelegate?.getListView()?.adapter = mAdapter
        getAllOrgData()
    }


    /**
     * 获取所有组织结构数据
     */
    private fun getAllOrgData() {
        val path = JniModuleAPI.orgTree
        val params = OrgRequestObj()
        BaseHttpRequest.getRequest(path, params, OrgBtQuResponseObj::class.java, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })


        mAdapter?.setOnTreeNodeClickListener(object : TreeListViewAdapter.OnTreeNodeClickListener {
            override fun onClick(node: Node?, position: Int) {
                if (node?.isLeaf!!) {
                    if (chooseType == 0) {
                        for (i in list.indices) {
                            list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                        }
                    } else if (chooseType == 1) {
                        //只可选街道
                        if (node.ztreeNodesBean.type == "1") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则街道")
                        }
                    } else if (chooseType == 2) {
                        //只可选社区
                        if (node.ztreeNodesBean.type == "2") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则社区")
                        }
                    } else if (chooseType == 3) {
                        //只可选则网格
                        if (node.ztreeNodesBean.type == "3") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则网格")
                        }
                    } else if (chooseType == 4) {
                        //只可选则单位
                        if (node.ztreeNodesBean.type == "4") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则单位")
                        }
                    } else if (chooseType == 6) {
                        //只可选则楼栋
                        if (node.ztreeNodesBean.type == "6") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则楼栋")
                        }
                    } else if (chooseType == 8) {
                        //只可选则单元中心户
                        if (node.ztreeNodesBean.type == "8") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则单元中心户")
                        }
                    }
                    mAdapter?.notifyDataSetChanged()
                }
            }
        })


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //确定
            var isCheck = false
            var orgId: String? = ""
            var orgName: String? = ""
            var orgPath: String? = ""
            var orgType: String? = ""
            for (i in list.indices) {
                val ztreeNodesBean = list[i].ztreeNodesBean
                if (ztreeNodesBean?.isCheck!!) {
                    orgId = ztreeNodesBean.id
                    orgName = ztreeNodesBean.name
                    orgPath = ztreeNodesBean.path
                    orgType = ztreeNodesBean.type
                    isCheck = true
                    break
                }
            }

            if (!isCheck) {
                LKToastUtil.showToastShort("请选择组织")
                return@OnClickListener
            }
            val intent = Intent()
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_ID, orgId)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME, orgName)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_TYPE, orgType)

            if (isAllPath) {
                if (!TextUtils.isEmpty(orgPath)) {
                    val split = orgPath!!.split("-")
                    var quName: String? = null
                    var jieDaoName: String? = null
                    var sheQuName: String? = null
                    var wangGeName: String? = null

                    for (i in 0 until split.size) {
                        when (i) {
                            0 -> {
                                quName = getOrgName(split[0])
                            }
                            1 -> {
                                jieDaoName = getOrgName(split[1])
                            }
                            2 -> {
                                sheQuName = getOrgName(split[2])
                            }
                            3 -> {
                                wangGeName = getOrgName(split[3])
                            }
                        }
                    }
                    intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_PARENT_PATH, orgPath)
                    val allName = quName + jieDaoName + sheQuName + wangGeName
                    intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_PARENT_ALL_NAME, allName)
                }
            }
            setResult(101, intent)
            finish()
        }, R.id.rl_orGan_submit)
    }


    private fun getOrgName(id: String): String {
        val name: String? = (0 until list.size)
                .firstOrNull { id == list[it].id }
                ?.let { list[it].ztreeNodesBean.name }
        return name + " "
    }

    override fun getDelegateClass(): Class<OrganizationActivityDelegate> {
        return OrganizationActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()


    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is OrgBtQuResponseObj -> {
                //所有宝塔区数据
                val data = obj.data
                val quId = data.id
                val quName = data.name
                val type = data.type
                val quParentId = data.parentId
                val path = data.path
                //街道
                val jieDaoChildren = data?.children
                val node = Node(quId, quParentId, ZtreeNodesBean(quId, quParentId, quName, "$type", path))
                list.add(node)
                getJieDao(jieDaoChildren)


                val listFilter = ArrayList<Node>()
                /**
                 * 宝塔区 type=0，街道type=1，社区type=2,网格type=3，单位type=4,
                 * 社会组织type=5，楼栋党小组type=6，党员type7，单元中心户type=8
                 */
                if (orgType == 0) {
                    listFilter.addAll(list)
                } else if (orgType == 1) {
                    //街道
                    for (i in 0 until list.size) {
                        if (list[i].getpId() == "0" || list[i].ztreeNodesBean?.type.equals("1")) {
                            listFilter.add(list[i])
                            continue
                        }
                    }
                } else if (orgType == 2) {
                    //社区
                    for (i in 0 until list.size) {
                        if ("0" == list[i].getpId() || "1" == list[i].ztreeNodesBean?.type ||
                                "2" == list[i].ztreeNodesBean?.type) {
                            listFilter.add(list[i])
                            continue
                        }
                    }
                } else if (orgType == 3) {
                    //网格
                    for (i in 0 until list.size) {
                        if ("0" == list[i].getpId() || "1" == list[i].ztreeNodesBean?.type ||
                                "2" == list[i].ztreeNodesBean?.type || "3" == list[i].ztreeNodesBean?.type) {
                            listFilter.add(list[i])
                            continue
                        }
                    }
                } else if (orgType == 4) {
                    //单位
                    for (i in 0 until list.size) {
                        if ("0" == list[i].getpId() || "1" == list[i].ztreeNodesBean?.type ||
                                "2" == list[i].ztreeNodesBean?.type || "3" == list[i].ztreeNodesBean?.type
                                || "4" == list[i].ztreeNodesBean?.type) {
                            listFilter.add(list[i])
                            continue
                        }
                    }
                } else if (orgType == 6) {
                    //楼栋
                    for (i in 0 until list.size) {
                        if ("0" == list[i].getpId() || "1" == list[i].ztreeNodesBean?.type ||
                                "2" == list[i].ztreeNodesBean?.type || "3" == list[i].ztreeNodesBean?.type ||
                                "6" == list[i].ztreeNodesBean?.type) {
                            listFilter.add(list[i])
                            continue
                        }
                    }
                } else if (orgType == 8) {
                    //楼栋
                    for (i in 0 until list.size) {
                        if ("0" == list[i].getpId() || "1" == list[i].ztreeNodesBean?.type ||
                                "2" == list[i].ztreeNodesBean?.type || "3" == list[i].ztreeNodesBean?.type ||
                                "6" == list[i].ztreeNodesBean?.type || "8" == list[i].ztreeNodesBean?.type) {
                            listFilter.add(list[i])
                            continue
                        }
                    }
                }
                mAdapter?.setNodeData(listFilter, 2)
                mAdapter?.notifyDataSetChanged()
            }
        }
    }

    /**
     * 获取街道数据
     */
    private fun getJieDao(children: ArrayList<JieDaoBean>?) {
        if (null != children && children.size > 0) {
            for (i in 0 until children.size) {
                //街道数据
                val objBean = children[i]
                val jieDaoId = objBean.id
                val jieDaoName = objBean.name
                val jieDaoParentId = objBean.parentId
                val type = objBean.type
                val path = objBean.path
                val node = Node(jieDaoId, jieDaoParentId, ZtreeNodesBean(jieDaoId, jieDaoParentId, jieDaoName, "$type", path))
                list.add(node)

                //社区数据
                val sheQuChildren = objBean.children
                getSheQu(sheQuChildren)
            }
        }
    }

    /**
     * 获取社区数据
     */
    private fun getSheQu(children: ArrayList<SheQuBean>?) {
        if (null != children && children.size > 0) {
            for (i in 0 until children.size) {
                //社区数据
                val objBean = children[i]
                val sheQuId = objBean.id
                val sheQuName = objBean.name
                val sheQuParentId = objBean.parentId
                val type = objBean.type
                val path = objBean.path
                val node = Node(sheQuId, sheQuParentId, ZtreeNodesBean(sheQuId, sheQuParentId, sheQuName, "$type", path))
                list.add(node)

                //网格数据
                val wangGeChildren = objBean.children
                getWangGe(wangGeChildren)
            }
        }
    }

    /**
     * 获取网格数据
     */
    private fun getWangGe(children: ArrayList<WangGeBean>?) {
        if (null != children && children.size > 0) {
            for (i in 0 until children.size) {
                //网格数据
                val objBean = children[i]
                val wangGeId = objBean.id
                val wangGeName = objBean.name
                val wangGeParentId = objBean.parentId
                val type = objBean.type
                val path = objBean.path
                val node = Node(wangGeId, wangGeParentId, ZtreeNodesBean(wangGeId, wangGeParentId, wangGeName, "$type", path))
                list.add(node)

                //楼栋数据
                val louDongChildren = objBean.children
                getLouDong(louDongChildren)
            }
        }
    }


    /**
     * 获取楼栋数据
     */
    private fun getLouDong(children: ArrayList<LouDongBean>?) {
        if (null != children && children.size > 0) {
            for (i in 0 until children.size) {
                //网格数据
                val objBean = children[i]
                val louDongId = objBean.id
                val louDongName = objBean.name
                val louDongParentId = objBean.parentId
                val type = objBean.type
                val path = objBean.path
                val node = Node(louDongId, louDongParentId, ZtreeNodesBean(louDongId, louDongParentId, louDongName, "$type", path))
                list.add(node)


                //单元中心户数据
                val dyhChildren = objBean.children
                getDanYuanHu(dyhChildren)
            }
        }
    }

    /**
     * 获取单元中心户数据(最后一级)
     */
    private fun getDanYuanHu(children: ArrayList<DanYuanHuBean>?) {
        if (null != children && children.size > 0) {
            for (i in 0 until children.size) {
                //网格数据
                val objBean = children[i]
                val danYuanHuId = objBean.id
                val danYuanHuName = objBean.name
                val danYuanHuParentId = objBean.parentId
                val type = objBean.type
                val path = objBean.path
                val node = Node(danYuanHuId, danYuanHuParentId, ZtreeNodesBean(danYuanHuId, danYuanHuParentId, danYuanHuName, "$type", path))
                list.add(node)
            }
        }
    }


    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
    }
}